#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 根据前序和中序二叉树遍历结果重建二叉树
void printVec(vector<int> &vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << "-";
    }
    cout << endl;
}
class Solution
{
public:
    TreeNode *reConstructBinaryTree(vector<int> pre, vector<int> vin)
    {
        if (pre.size() == 1)
        {
            return new TreeNode(pre[0]);
        }

        TreeNode *root = new TreeNode(pre[0]);
        int index;
        for (int i = 0; i < vin.size(); i++)
        {
            if (vin[i] == pre[0])
            {
                index = i;
                break;
            }
        }
        vector<int> vl(vin.begin(), vin.begin() + index);

        vector<int> vr(vin.begin() + index + 1, vin.end());

        vector<int> pl(pre.begin() + 1, pre.begin() + 1 + vl.size());

        vector<int> pr(pre.begin() + vl.size() + 1, pre.end());

        if (pl.size() != 0)
        {
            root->left = reConstructBinaryTree(pl, vl);
        }
        if (pr.size() != 0)
        {
            root->right = reConstructBinaryTree(pr, vr);
        }
        return root;
    }
};

void printNode(TreeNode *root)
{
    if (root == NULL)
        return;
    cout << root->val << ",";
    printNode(root->left);
    printNode(root->right);
}

int main()
{
    Solution s;
    vector<int> pre({1, 2, 3, 4});
    vector<int> vin({1,2,3,4});
    TreeNode *root = s.reConstructBinaryTree(pre, vin);
    cout << "9999" << endl;
    printNode(root);
    return 0;
}